package com.party.core.service.circle.biz;

import com.party.common.utils.PartyCode;
import com.party.core.exception.BusinessException;
import com.party.core.model.BaseModel;
import com.party.core.model.YesNoStatus;
import com.party.core.model.circle.Circle;
import com.party.core.model.circle.CircleApply;
import com.party.core.model.circle.CircleCode;
import com.party.core.model.circle.CircleMember;
import com.party.core.model.member.PartnerMember;
import com.party.core.service.circle.ICircleApplyService;
import com.party.core.service.circle.ICircleMemberService;
import com.party.core.service.circle.ICircleService;
import com.party.core.service.member.IPartnerMemberService;
import com.party.core.service.member.biz.MemberPersonalCountBizService;
import com.party.core.service.member.util.MemberFormUtil;
import com.party.core.service.system.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.function.Function;

/**
 * 圈子申请业务逻辑接口 party Created by Juliana on 2016-12-14
 */

@Service
public class CircleApplyBizService {
    @Autowired
    private ICircleMemberService circleMemberService;
    @Autowired
    private ICircleApplyService circleApplyService;
    @Autowired
    private CircleMemberBizService circleMemberBizService;
    @Autowired
    private ICircleService circleService;
    @Autowired
    private IPartnerMemberService partnerMemberService;
    @Autowired
    private MemberPersonalCountBizService memberPersonalCountBizService;
    @Autowired
    private ISysConfigService sysConfigService;

    public Integer getApplyData(String memberId, CircleApply apply, Function fn) {
        Circle circle = circleService.get(apply.getCircle());
        if (null == circle || BaseModel.DEL_FLAG_DELETE.equals(circle.getDelFlag())) {
            throw new BusinessException(PartyCode.IS_NO_DATA, "圈子数据不存在");
        }

        if (sysConfigService.isOpenExpirationRestriction()) {
            boolean isExpire = MemberFormUtil.isExpire(circle.getCreateBy(), null);
            if (isExpire) {
                throw new BusinessException("该商户歇业中，无法加入");
            }
        }

        //查询当前用户是否已经为圈子成员
        CircleMember cmSearch = new CircleMember();
        cmSearch.setMember(memberId);
        cmSearch.setCircle(apply.getCircle());
        CircleMember circleMember = circleMemberService.getUnique(cmSearch);
        if (null == circleMember) {
            CircleApply search = new CircleApply();
            search.setMember(memberId);
            search.setCircle(apply.getCircle());
            CircleApply dbCircleApply = circleApplyService.getUnique(search);
            if (null == dbCircleApply) {
                // 没有报名，则新建报名信息
                dbCircleApply = new CircleApply();
                dbCircleApply.setCircle(apply.getCircle());
                dbCircleApply.setCreateBy(memberId);
                dbCircleApply.setMember(memberId);
                dbCircleApply.setRemarks(apply.getRemarks());
                if (YesNoStatus.YES.getCode().equals(circle.getIsAudit())) {
                    dbCircleApply.setCheckStatus(CircleCode.Apply_STATUS_AUDITING.getCode());
                } else {
                    dbCircleApply.setCheckStatus(CircleCode.Apply_STATUS_AUDIT_PASS.getCode());
                }
                circleApplyService.insert(dbCircleApply);
            } else {
                if (YesNoStatus.YES.getCode().equals(circle.getIsAudit())) {
                    dbCircleApply.setCheckStatus(CircleCode.Apply_STATUS_AUDITING.getCode());
                } else {
                    dbCircleApply.setCheckStatus(CircleCode.Apply_STATUS_AUDIT_PASS.getCode());
                }
                dbCircleApply.setRemarks(apply.getRemarks());
                circleApplyService.update(dbCircleApply);
            }

            if (CircleCode.Apply_STATUS_AUDIT_PASS.getCode().equals(dbCircleApply.getCheckStatus())) {
                applyPass(dbCircleApply, memberId);
            }

            // 机构用户关系
            PartnerMember partnerMember = new PartnerMember();
            String partnerId = circleService.get(apply.getCircle()).getCreateBy();
            partnerMember.setPartnerId(partnerId);
            partnerMember.setMemberId(memberId);
            PartnerMember pm = partnerMemberService.getSet(partnerMember);
            if (null != pm && null != fn) {
                fn.apply(pm);
            }
            // 个人统计表添加数据
            memberPersonalCountBizService.getSet(partnerId, memberId);

            return circle.getIsAudit();
        } else {
            throw new BusinessException("您已经加入圈子了");
        }
    }

    /**
     * 拒绝
     *
     * @param id
     * @param curUserId
     */
    public void reject(String id, String curUserId) {
        CircleApply dbApply = circleApplyService.get(id);
        if (dbApply == null) {
            throw new BusinessException("找不到申请关系");
        } else {
            dbApply.setUpdateBy(curUserId);
            dbApply.setCheckStatus(CircleCode.Apply_STATUS_AUDIT_REJECT.getCode());
            circleApplyService.update(dbApply);

            // 更新圈子会员数
            circleMemberBizService.updateMemberNum(dbApply.getCircle());
        }
    }

    /**
     * 通过
     *
     * @param id
     * @param curUserId
     */
    public void pass(String id, String curUserId) {
        CircleApply dbApply = circleApplyService.get(id);
        if (dbApply == null) {
            throw new BusinessException("找不到申请关系");
        } else {
            dbApply.setUpdateBy(curUserId);
            dbApply.setCheckStatus(CircleCode.Apply_STATUS_AUDIT_PASS.getCode());
            circleApplyService.update(dbApply);

            applyPass(dbApply, curUserId);
        }
    }

    /**
     * 审核通过，成为圈子成员
     *
     * @param apply    报名记录
     * @param memberId 用户id
     */
    public void applyPass(CircleApply apply, String memberId) {
        CircleMember sCircleMember = new CircleMember();// 检索对象
        sCircleMember.setCircle(apply.getCircle());
        sCircleMember.setMember(apply.getMember());
        CircleMember dbCircleMember = circleMemberService.getUnique(sCircleMember);
        if (dbCircleMember == null) {
            circleMemberBizService.saveBiz(apply.getCircle(), apply.getMember(), memberId, 3); // 前端申请生成圈子成员
        }
    }
}
